package com.zzit.vjv.exception;

import com.zzit.vjv.exception.CustomException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义异常处理器
 */
public class CustomExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {


        e.printStackTrace();

        //声明异常类
        CustomException customException = null;
        //如果抛出的是系统自定义异常，则直接转换
        if (e instanceof CustomException) {

            customException = (CustomException) e;

            System.out.println("进来啦兄弟们1");

        } else {

            //如果抛出的不是系统自定义异常则重新构造一个系统错误异常。
            customException=new CustomException("系统错误，请与系统管理 员联系！");

            System.out.println("进来啦兄弟们2");

        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", customException.getMessage());
        modelAndView.setViewName("error");


        return modelAndView;

    }
}
